﻿/* **************************************************************************
 *
 * Copyright 2007-2010 Jeff Hardy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * *************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Web;

namespace NWSGI.AspNet
{
    class WsgiAspNetRequest : IWsgiRequest
    {
        private HttpRequest request;
        private bool wildcard;

        public WsgiAspNetRequest(HttpContext context, bool wildcard)
        {
            this.request = context.Request;
            this.wildcard = wildcard;
        }

        public string Method
        {
            get { return request.HttpMethod; }
        }

        public string ScriptName
        {
            get { return wildcard ? request.ApplicationPath : request.FilePath; }
        }

        public string PathInfo
        {
            get { return wildcard ? request.FilePath.Substring(request.ApplicationPath.Length) : request.PathInfo; }
        }

        public string QueryString
        {
            get { return request.QueryString.ToString(); }
        }

        public string ContentType
        {
            get { return request.ContentType; }
        }

        public string ContentLength
        {
            get { return request.ContentLength.ToString(); }
        }

        public string ServerName
        {
            get { return request.ServerVariables["SERVER_NAME"]; }
        }

        public string ServerPort
        {
            get { return request.ServerVariables["SERVER_PORT"]; }
        }

        public string ServerProtocol
        {
            get { return request.ServerVariables["SERVER_PROTOCOL"]; }
        }

        public IEnumerable<KeyValuePair<string, string>> Headers
        {
            get 
            {
                foreach(string key in request.Headers)
                {
                    yield return new KeyValuePair<string, string>(key, request.Headers[key]);
                }
            }
        }

        public IEnumerable<KeyValuePair<string, string>> CgiServerVariables
        {
            get 
            {
                foreach(string key in request.ServerVariables)
                {
                    yield return new KeyValuePair<string, string>(key, request.ServerVariables[key]);
                }
            }
        }

        public Uri Url
        {
            get { return request.Url; }
        }

        public string RawUrl
        {
            get { return request.RawUrl; }
        }

        public Stream InputStream
        {
            get { return request.InputStream; }
        }
    }
}
